package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SysMenu;
import com.easy.common.service.IMenuService;
import com.easy.user.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;

@Service
public class MenuService implements IMenuService {
    @Autowired
    MenuMapper menuMapper;
    @Override
    public SysMenu insert(SysMenu menu) {
        menuMapper.insert(menu);
        SysMenu result=this.getByID(menu.getId());
        return result;
    }

    @Override
    public SysMenu update(SysMenu menu) {
        menuMapper.updateById(menu);
        SysMenu result=this.getByID(menu.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=menuMapper.deleteById(id);
        return num;
    }

    @Override
    public SysMenu getByID(Integer id) {
        SysMenu result=menuMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<SysMenu> getListByPage(IPage page, SysMenu menu) {
        Wrapper wrapper=new QueryWrapper(menu);
        IPage result=menuMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public int setVisible(Integer id) {
        SysMenu menu = menuMapper.selectById(id);
        if (menu != null) {
            int newVisible = menu.getVisible() == 1 ? 0 : 1;
            UpdateWrapper<SysMenu> wrapper = new UpdateWrapper<>();
            wrapper.set("visible", newVisible).eq("id", id);
            return menuMapper.update(null, wrapper);
        }
        return 0;
    }

    @Override
    public List<SysMenu> getList(SysMenu menu) {
        Wrapper wrapper=new QueryWrapper(menu);
        List<SysMenu> result=menuMapper.selectList(wrapper);
        return result;
    }
}
